// pages/home/home.js
// var api = require('../../requests/api.js');
let cache = require('/../../utils/cache.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasUserInfo: true,//验证手机号
    userinfos: true, //验证微信授权
    avatarUrl: "",//用户的头像
    nickName:'',//用户的名字
    contentlists: false,
    couList: [],
    test:false,
    couponCount: 0, //优惠券
    integral: 0, //积分
    userAfterCardCount: 0,//售后卡
    msg:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function () {
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    if (app.globalData.employId && app.globalData.employId != '') {
      wx.getStorage({
        key: 'openId',
        success: res => {
          let openId = res.data
          // 获取用户的信息
          wx.request({
            url: 'https://www.jiujiangmm.com/pub/login/getUserData',
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            data: {
              openId: openId
            },
            success: res => {
              let token = res.data.data.token
              debugger
              that.setData({
                avatarUrl: res.data.data.userInfo.avatarUrl,
                nickName: res.data.data.userInfo.nickName,
                userinfos: false
              })
              if (res.data.data.phone != "" && res.data.data.userInfo  ) {
                // 获取用户的信息
                wx.request({
                  url: 'https://www.jiujiangmm.com/api/coupon/getCouponPromotionsList',
                  method: "POST",
                  header: {
                    'content-type': 'application/x-www-form-urlencoded',
                    "token": token
                  },
                  success: res => {
                    wx.hideLoading();
                    if (res.data.data.pospalDataList.length > 0) {
                      that.setData({
                        couList: res.data.data.pospalDataList,
                        couponCount: res.data.data.couponCount,
                        integral: res.data.data.integral,
                        userAfterCardCount: res.data.data.userAfterCardCount,
                        hasUserInfo: false
                      })
                    } else {
                      that.setData({
                        contentlists: true,
                        couponCount: res.data.data.couponCount,
                        integral: res.data.data.integral,
                        userAfterCardCount: res.data.data.userAfterCardCount,
                        hasUserInfo: false
                      })
                    }
                  }
                })

              } else {
                wx.request({
                  url: 'https://www.jiujiangmm.com/pub/login/getCouponPromotionsList',
                  method: "POST",
                  header: {
                    'content-type': 'application/x-www-form-urlencoded',
                  },
                  success: res => {
                    wx.hideLoading();
                    if (res.data.data.length != 0) {

                      that.setData({
                        couList: res.data.data
                      })
                    } else {
                 
                      that.setData({
                        contentlists: true
                      })
                    }
                  }
                })
              }
            }
          })
        }
      })
    } else {
      app.employIdCallback = checkLogin => {
        if (checkLogin != '') {
          wx.getStorage({
            key: 'openId',
            success: res => {
              let openId = res.data
              // 获取用户的信息
              wx.request({
                url: 'https://www.jiujiangmm.com/pub/login/getUserData',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded',
                },
                data: {
                  openId: openId
                },
                success: res => {
                  let token = res.data.data.token
                  userinfos: true
                  if (res.data.data.phone != "") {
                    // 获取用户的信息
                    wx.request({
                      url: 'https://www.jiujiangmm.com/api/coupon/getCouponPromotionsList',
                      method: "POST",
                      header: {
                        'content-type': 'application/x-www-form-urlencoded',
                        "token": token
                      },
                      success: res => {
                        wx.hideLoading();
               
                        if (res.data.data.pospalDataList.length > 0) {
                          that.setData({
                            couList: res.data.data.pospalDataList,
                            couponCount: res.data.data.couponCount,
                            integral: res.data.data.integral,
                            userAfterCardCount: res.data.data.userAfterCardCount,
                            hasUserInfo: false
                          })
                        } else {
                          that.setData({
                            contentlists: true,
                            couponCount: res.data.data.couponCount,
                            integral: res.data.data.integral,
                            userAfterCardCount: res.data.data.userAfterCardCount,
                            hasUserInfo: false
                          })
                        }
                      }
                    })

                  } else {
                    wx.request({
                      url: 'https://www.jiujiangmm.com/pub/login/getCouponPromotionsList',
                      method: "POST",
                      header: {
                        'content-type': 'application/x-www-form-urlencoded',
                      },
                      success: res => {
                        wx.hideLoading();
                   
                        if (res.data.data.length != 0) {
                          that.setData({
                            couList: res.data.data
                          })
                        } else {
                          that.setData({
                            contentlists: true
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          })
        }
      }
    }
  },

  onReady: function () {

  },
  binlicks(e) {
    app.globalData
    wx.redirectTo({
      url: '/pages/logs/logs',
    })
  },
  // 跳转小程序
  btns(){
    wx.navigateToMiniProgram({
      appId: 'wx2e699977257cd9ce',
      path: 'pages/common/blank-page/index?weappSharePath=pages%2Fhome%2Fdashboard%2Findex%3Fkdt_id%3D92315128',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },
  // 领取优惠券
  binlick(e) {
    wx.getStorage({
      key: 'openId',
      success: res => {
        let openId = res.data
        // 获取用户的信息
        wx.request({
          url: 'https://www.jiujiangmm.com/pub/login/getUserData',
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            openId: openId
          },
          success: res => {
            // 判断是否授权过手机号
            let token = res.data.data.token
            if (res.data.data.userInfo === '') {
              // debugger
              wx.redirectTo({
                url: '/pages/logs/logs',
              })
            } else if (res.data.data.phone === "") {
              wx.redirectTo({
                url: '/pages/mobile/mobile',
              })
            } else if (res.data.data.phone != "" && res.data.data.userInfo != '') {
              wx.redirectTo({
                url: '/pages/receive/receive?couponDetail=' + e.currentTarget.dataset.rule + '&token=' + token + '&promotionCouponUid=' + e.currentTarget.id,
              })
            }
          }
        })
      }
    })

  },
  // 进去售后卡
  after_sale() {
    wx.showLoading({
      title: '加载中',
    })
    wx.getStorage({
      key: 'openId',
      success: res => {
        let openId = res.data
        // 获取用户的信息
        wx.request({
          url: 'https://www.jiujiangmm.com/pub/login/getUserData',
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            openId: openId
          },
          success: res => {
            wx.hideLoading();
            console.log(res.data.data.phone)
            if (res.data.data.userInfo === '' ) {
              wx.redirectTo({
                url: '/pages/logs/logs',
              })
            } else if (res.data.data.userInfo != '' && res.data.data.phone != "") {
              wx.navigateTo({
                url: '/pages/index/index',
              })
            } else if (res.data.data.phone === "") {
              wx.redirectTo({
                url: '/pages/mobile/mobile',
              })
            }
          }
        })
      }
    })

  },

  // 查看优惠券
  coupons() {
    wx.getStorage({
      key: 'openId',
      success: res => {
        let openId = res.data
        // 获取用户的信息
        wx.request({
          url: 'https://www.jiujiangmm.com/pub/login/getUserData',
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            openId: openId
          },
          success: res => {
            if (res.data.data.userInfo === '') {
              wx.redirectTo({
                url: '/pages/logs/logs',
              })
            } else if (res.data.data.userInfo != null  && res.data.data.phone != "") {
              wx.navigateTo({
                url: '/pages/coupon/coupon',
              })
            } else if (res.data.data.userInfo != '' ){
              wx.redirectTo({
                url: '/pages/mobile/mobile',
              })
            }
          }
        })
      }
    })

  },

  // 使用优惠券
  binlickemploy: function (e) {
    let code = e.currentTarget.dataset.code
    let name = e.currentTarget.dataset.name
    let validity = e.currentTarget.dataset.validity
    let showCode = e.currentTarget.dataset.showcode
    wx.redirectTo({
      url: '/pages/coding/coding?code=' + code + '&name=' + name + '&validity=' + validity + '&showCode=' + showCode,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
    // wx.hideNavigationBarLoading(); //完成停止加载图标
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})